package com.android.geotrack.artist;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import com.android.geotrack.GeoTrack;
import com.android.geotrack.R;
import com.android.geotrack.R.drawable;
import com.android.geotrack.R.string;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Message;
import android.util.Log;

public class LoadArtistsRunnable implements Runnable
{
	private Activity caller;
	public volatile static boolean lar_started = false;
	private String user;
	
	public LoadArtistsRunnable(Activity caller, String user)
	{
		this.caller = caller;
		this.user = user;
	}
	
	public void run()
	{
		this.lar_started = true;
		
		List<Artist> artists = new LinkedList<Artist>();
		
		/* Start parsing. */
		try {
			
			Log.v("LoadArtistsRunnable", "Started parsing.");
			
			// Make URL.
			URL url = new URL("http://" 
					+ caller.getResources().getString(R.string.host) 
					+ "/related_artists.php?user1="
					+ GeoTrack.currentUser.getName()
					+ "&user2="
					+ user);
			
	        // Get a SAXParser from the SAXPArserFactory.
	        SAXParserFactory spf = SAXParserFactory.newInstance();
	        SAXParser sp = spf.newSAXParser();

	        // Get the XMLReader of the SAXParser we created.
	        XMLReader xr = sp.getXMLReader();
	        // Create a new ContentHandler and apply it to the XML-Reader.
	        ArtistParser parser = new ArtistParser();
	        xr.setContentHandler(parser);
	        
	        // Parse the xml-data from our URL.
			Log.v("Connecting...", url.toString());
	        xr.parse(new InputSource(url.openStream()));
			
			Log.v("LoadArtistsRunnable", "Parsing finished.");
	        
	        /* Parsing has finished. */
			artists = parser.getParseResult();

		} catch (ParserConfigurationException e) {
			Log.e("ParserConfigurationException", e.toString());
		} catch (SAXException e) {
			Log.e("SAXException", e.toString());
		} catch (IOException e) {
			Log.e("IOException", e.toString());
		}
			
		// Draw thumbnails
		LinkedList<Drawable> thumbs = new LinkedList<Drawable>();
		for (Artist a : artists) {
			String image = a.getThumbnail_medium();
			if (image!=null && ! image.equals("")) {
				try {
					thumbs.add(Drawable.createFromStream(new URL(image).openStream(), ""));
				} catch (MalformedURLException e) {
					Log.e("MalformedURLException", e.toString());
				} catch (IOException e) {
					Log.e("IOException", e.toString());
				}
			} else {
				thumbs.add(caller.getResources().getDrawable(R.drawable.user));
			}
		}
			
		// Send results
		Message msg = ((ArtistsActivity)caller).messageHandler.obtainMessage();
		msg.what = 42;
		Object [] results = new Object[2];
		results[0] = artists;
		results[1] = thumbs;
		msg.obj = results;
		((ArtistsActivity)caller).messageHandler.sendMessage(msg);
		
		this.lar_started = false;
	}

}